//
//  XianjiTestViewController.m
//  MyFishHelper
//
//  Created by EmberWei on 2018/8/14.
//  Copyright © 2018年 EmberWei. All rights reserved.
//

#import "XianjiTestViewController.h"

@interface XianjiResCell :UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *lbResETH;
@property (weak, nonatomic) IBOutlet UILabel *lbResTimes;
@property (weak, nonatomic) IBOutlet UILabel *lbResPercent;
@end

@implementation XianjiResCell

@end

@interface XianjiTestViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIImageView *icon;
@property (weak, nonatomic) IBOutlet UILabel *lbName;
@property (weak, nonatomic) IBOutlet UILabel *lbEth;
@property (weak, nonatomic) IBOutlet UILabel *lbLevel;
@property (weak, nonatomic) IBOutlet UILabel *lbTimeCount;
@property (nonatomic,strong) NSMutableArray<ResponseOffGo*> *resultList;
@property (nonatomic,assign) NSInteger leijiCount;
@property (weak, nonatomic) IBOutlet UILabel *lbLeijiYingkui;

@property (assign, nonatomic) CGFloat leijiYingkui;
@end

@implementation XianjiTestViewController

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.resultList count];
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ResponseOffGo *go = self.resultList[indexPath.row];
    XianjiResCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XianjiResCell"];
    cell.lbResETH.text = go.eth;
    cell.lbResTimes.text = FORMAT(@"%td",go.item_times);
    cell.lbResPercent.text = go.percent;
    return cell;
}

-(void)loadData {
    self.lbLevel.text = FORMAT(@"L%@",self.item.level);
    self.lbName.text = self.item.name;
    [self.icon sd_setImageWithURL:[NSURL URLWithString:self.item.img]];
    CGFloat f  = [self.item.equal_eth floatValue];
    NSString *eth = FORMAT(@"%0.3fETH",f);
    self.lbEth.text = eth;
    self.title = @"献祭统计";
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.leijiCount = 0;
    self.leijiYingkui = 0.0f;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.resultList = [[NSMutableArray alloc]init];
    [self loadData];
}

- (IBAction)actionClick:(UIButton*)button {
    self.resultList = [[NSMutableArray alloc]init];
    WEAK(self)
    NSInteger time = button.tag;
     __block NSInteger cnt = time;
    [MFHUDManager showLoading:@"一键献祭中...."];
    for (int i = 0 ; i < time; i++) {
        STRONG(self)
        [Api apiOfferGoWithFishId:@(0) fishLevel:self.item.level handler:^(ResponseOffGo *result, NSError *error) {
            if (error) {
                [MFHUDManager showError:@"请检查网络,稍后再试"];
                return;
            }
            if (![result.code isEqualToString:@"0"]) {
                [MFHUDManager showError:result.msg];
                return;
            }
            cnt--;
            ResponseOffGo *go = result;
            if (go) {
                [self.resultList addObject:go];
            }
            if (cnt == 0) {
                NSMutableString *string = [[NSMutableString alloc]initWithString:@"一键献祭成功!"];
                [MFHUDManager showSuccess:string];
                self.resultList = [self resultTableArrayWithPercent:[self resultTableArray]];
                [self.tableView reloadData];
                self.leijiCount += time;
                // self.lbTimeCount.text = FORMAT(@"%td",self.leijiCount);
                if ([self isWin:time] == 1) {
                    self.lbTimeCount.textColor = [UIColor redColor];
                }
                else if ([self isWin:time] == 0)
                {
                    self.lbTimeCount.textColor = [UIColor greenColor];
                }
                else
                {
                    self.lbTimeCount.textColor = [UIColor whiteColor];
                }
                self.lbTimeCount.text = [self yingkuiString:time];
                CGFloat benci = [self yingkuiFloat:time];
                self.leijiYingkui += benci ;
                self.lbLeijiYingkui.text = FORMAT(@"%0.3f ETH",self.leijiYingkui);
                
                if (self.leijiYingkui > 0) {
                    self.lbLeijiYingkui.textColor = [UIColor redColor];
                }
                else if (self.leijiYingkui < 0)
                {
                    self.lbLeijiYingkui.textColor = [UIColor greenColor];
                }
                else
                {
                    self.lbLeijiYingkui.textColor = [UIColor whiteColor];
                }
                
            }
        }];
    }
}

-(CGFloat)calSum
{
    CGFloat sum = 0.0f;
    for (ResponseOffGo *go in self.resultList) {
        CGFloat f = [go.eth floatValue] * go.item_times;
        sum+= f;
    }
    return sum;
}

-(CGFloat)yingkuiFloat:(NSInteger)times
{
    CGFloat chanchu = [self calSum];
    CGFloat touru =   [self.item.equal_eth floatValue] * times;
    return (chanchu - touru);
}

-(NSString*)yingkuiString:(NSInteger)times
{
    CGFloat chanchu = [self calSum];
    CGFloat touru =   [self.item.equal_eth floatValue] * times;
    if (chanchu < touru) {
        return FORMAT(@"%0.3f ETH",touru - chanchu);
    }
    else if (chanchu > touru)
    {
        return FORMAT(@"%0.3f ETH",chanchu - touru);
    }
    else
    {
        return @"0 ETH";
    }
}

-(NSInteger)isWin:(NSInteger)times
{
    CGFloat chanchu = [self calSum];
    CGFloat touru =   [self.item.equal_eth floatValue] * times;
    if (chanchu < touru) {
        return 0;
    }
    else if (chanchu > touru)
    {
        return 1;
    }
    else
    {
        return 2;
    }
}


-(BOOL)isHaveSame:(ResponseOffGo*)newGo goLists:(NSMutableArray<ResponseOffGo*>*)goLists {
    NSString *eth = newGo.eth;
    for (ResponseOffGo *go in goLists) {
        if ([eth isEqualToString:go.eth]) {
            return YES;
        }
    }
    return NO;
}

-(ResponseOffGo*)getSame:(ResponseOffGo*)newGo fishAwards:(NSMutableArray<ResponseOffGo*>*)fishAwards {
    NSString *eth = newGo.eth;
    for (ResponseOffGo *go in fishAwards) {
        if ([eth isEqualToString:go.eth]) {
            return go;
        }
    }
    return nil;
}

-(NSMutableArray*)resultTableArray
{
    NSMutableArray *resArr = [[NSMutableArray alloc]init];
    for (ResponseOffGo *go in self.resultList) {
        BOOL isSame = [self isHaveSame:go goLists:resArr];
        if (!isSame) {
            go.item_times = 1;
            [resArr addObject:go];
        }
        else {
            ResponseOffGo *same = [self getSame:go fishAwards:resArr];
            same.item_times += 1;
        }
    }
    return resArr;
}

-(NSMutableArray*)resultTableArrayWithPercent:(NSMutableArray*)oldFishAwards {
    NSInteger count = [self.resultList count];
    NSMutableArray *resArr = [[NSMutableArray alloc]init];
    for (ResponseOffGo *go in oldFishAwards) {
        CGFloat percentf = ((go.item_times *1.0f) / count) * 100.f;
        NSString *percent = FORMAT(@"%0.2f%%",percentf);
        go.percent = percent;
        [resArr addObject:go];
    }
    return resArr;
}

@end
